// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using LABSMFController.LABS.Classses;
using LABSMFController.LABS.Devices;

namespace LABSMFController.LABS.Steps
{
    internal class PumpSteps
    {
        internal PumpSteps(int stepNum, string name, string description)
        {
            Valves = new BeerDevices.ValveDevice[BeerDevices.MAXValves];
            //  Kettles = new Devices.BeerDevices.KettlesDevice[3];
            Bubblers = new BubblerDevice[MAXBubblers];
            TempProbes = new BeerDevices.TempProbe[BeerDevices.MAXProbes];
            Pumps = new BeerDevices.PumpDevice[BeerDevices.MAXPumps];
            VValves = new VariableValveDevice[BeerDevices.MAXVValves];
            VValveLevel = new int[BeerDevices.MAXVValves];
            Name = name;
            Description = description;
            StepNum = stepNum;
            ValveCount = 0;
            DurationToRun = 0;
            BubblerID = -1;
            BubblerGallons = 0;
            BubblerEmpty = false;
            VValveCount = 0;
            Header = "Description,IsActive,BubblerID,BubblerGallons,Name,StepNum,ConflictsWith";
        }

        public bool IsActive { get; set; }
        public bool BubblerEmpty { get; set; }
        public string Name { get; private set; }
        public string ConflictsWith { get; set; }
        public string Header { get; set; }
        public BeerDevices.ValveDevice[] Valves { get; private set; }
        public VariableValveDevice[] VValves { get; private set; }
        public int[] VValveLevel { get; private set; }
        //    public Devices.BeerDevices.KettlesDevice[] Kettles { get; private set; }
        public BubblerDevice[] Bubblers { get; private set; }
        public BeerDevices.TempProbe[] TempProbes { get; private set; }
        public BeerDevices.PumpDevice[] Pumps { get; private set; }
        public double DurationToRun { get; internal set; }
        public int BubblerID { get; internal set; }
        public int BubblerGallons { get; internal set; }
        public int ValveCount { get; private set; }
        public int VValveCount { get; private set; }
        public int PumpCount { get; private set; }

        public string Description { get; set; }
        public int StepNum { get; set; }

        public string GetClientOutPut()
        {
            string[] c = ConflictsWith.Split(',');
            string rr = c[0];
            for (int index = 1; index < c.Length; index++)
            {
                string t = c[index];
                rr = rr + "_" + t;
            }

            string ret = Description + "," + IsActive + "," + BubblerID + "," + BubblerGallons + "," + Name + "," +
                         StepNum + "," + rr;
            return ret;
        }

        public void AddVValve(VariableValveDevice vvalveDevice, int level)
        {
            VValveLevel[VValveCount] = level;
            VValves[VValveCount++] = vvalveDevice;
        }

        public void AddValve(BeerDevices.ValveDevice valveDevice)
        {
            Valves[ValveCount++] = valveDevice;
        }

        public void AddPump(BeerDevices.PumpDevice pumpDevice)
        {
            Pumps[PumpCount++] = pumpDevice;
        }
    }

    internal static class BeerSteps
    {
        static BeerSteps()
        {
            if (Steps == null)
                InitSteps();
        }

        public static PumpSteps[] Steps { get; set; }

        public static void InitSteps()
        {
            if (Steps != null)
                return;

            BeerDevices.MAXSteps = 11;
            Steps = new PumpSteps[BeerDevices.MAXSteps];

            Steps[0] = new PumpSteps(0, "Step 0", "Cycle Water in HLT");
            Steps[0].AddValve(BeerDevices.Valves[1]);
            Steps[0].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing); //3-1
            Steps[0].AddPump(BeerDevices.Pumps[1]);
            Steps[0].ConflictsWith = "3";

            Steps[1] = new PumpSteps(1, "Step 1", "Cycle Water in BK");
            Steps[1].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].BSwing);
            Steps[1].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].BSwing);
            Steps[1].AddPump(BeerDevices.Pumps[0]);
            Steps[1].ConflictsWith = "4";


            Steps[2] = new PumpSteps(2, "Step 2", "Cycle Water in From MLT through HLT Coil");
            Steps[2].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing); //3-1
            Steps[2].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].ASwing); //3-2
            Steps[2].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].ASwing); //3-3
            Steps[2].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].BSwing); //3-4            
            Steps[2].AddPump(BeerDevices.Pumps[0]);
            Steps[2].ConflictsWith = "1,3,4";


            Steps[3] = new PumpSteps(3, "Step 3", "Empty water from HLT to MLT");
            Steps[3].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].BSwing);
            Steps[3].AddPump(BeerDevices.Pumps[1]);
            Steps[3].BubblerEmpty = true;
            Steps[3].BubblerID = Location.HLT;
            Steps[3].BubblerGallons = 0;
            Steps[3].ConflictsWith = "0,2";

            Steps[4] = new PumpSteps(4, "Step 4", "Empty water from MLT to Fermenter");
            //Steps[4].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing);//3-1
            Steps[4].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].ASwing); //3-2
            Steps[4].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].ASwing); //3-3
            Steps[4].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].ASwing); //3-4
            Steps[4].AddPump(BeerDevices.Pumps[0]);
            Steps[4].BubblerEmpty = true;
            Steps[4].BubblerID = Location.MLT;
            Steps[4].BubblerGallons = 0;
            Steps[4].ConflictsWith = "1,2";

            Steps[5] = new PumpSteps(5, "Step 5", "Empty water from BK to Fermenter");
            //Steps[4].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing);//3-1
            Steps[5].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].BSwing); //3-2
            Steps[5].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].ASwing); //3-3
            Steps[5].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].ASwing); //3-4
            Steps[5].AddPump(BeerDevices.Pumps[0]);
            Steps[5].BubblerEmpty = true;
            Steps[5].BubblerID = Location.BK;
            Steps[5].BubblerGallons = 0;
            Steps[5].ConflictsWith = "1,2,4";


            Steps[6] = new PumpSteps(6, "Step 6", "Empty water from MLT to BK");
            //Steps[6].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing);//3-1
            Steps[6].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].ASwing); //3-2
            Steps[6].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].BSwing); //3-3
            //Steps[6].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].ASwing);//3-4
            Steps[6].AddPump(BeerDevices.Pumps[0]);
            Steps[6].BubblerEmpty = true;
            Steps[6].BubblerID = Location.MLT;
            Steps[6].BubblerGallons = 0;
            Steps[6].ConflictsWith = "1,2,4,5";


            Steps[7] = new PumpSteps(7, "Step 7", "Move BK to MLT");
            Steps[7].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing); //3-1
            Steps[7].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].BSwing); //3-2
            Steps[7].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].ASwing); //3-3
            Steps[7].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].BSwing); //3-4
            Steps[7].ConflictsWith = "1,2,4,5,6";
            Steps[7].BubblerEmpty = true;
            Steps[7].BubblerID = Location.BK;
            Steps[7].BubblerGallons = 0;
            Steps[7].AddPump(BeerDevices.Pumps[0]);

            Steps[8] = new PumpSteps(8, "Step 8", "Cycle water from BK to Fermenter");
            //Steps[4].AddVValve(VValveDevices.VValves[2], VValveDevices.VValves[2].ASwing);//3-1
            Steps[8].AddVValve(VValveDevices.VValves[3], VValveDevices.VValves[3].BSwing); //3-2
            Steps[8].AddVValve(VValveDevices.VValves[4], VValveDevices.VValves[4].ASwing); //3-3
            Steps[8].AddVValve(VValveDevices.VValves[5], VValveDevices.VValves[5].ASwing); //3-4
            Steps[8].AddPump(BeerDevices.Pumps[0]);
            Steps[8].ConflictsWith = "1,2,4";
        }
    }
}